﻿Imports System.Web.UI
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.BusinessLayer.Views
Imports SISSAT.WebDevelopment.Views

Namespace [Shared].UserControls
    Public Class CourseDetailControl
        Inherits UserControl
        Implements ICourseDetailControlView

#Region "Fields"

        Private _controlMode As PageModes

#End Region

#Region "Properties"

        Public Property ErrorMessage As String
        Public Property UseControlModeFromUserInterface As Boolean

        Public Property ControlMode As PageModes
            Get
                Return _controlMode
            End Get
            Set(value As PageModes)
                If UseControlModeFromUserInterface Then
                    Exit Property
                End If

                _controlMode = value
                IsReadOnly = Not (_controlMode = PageModes.EditMode)
                CssClass = Convert.ToString(IIf(IsReadOnly, "textbox readonly", "textbox req"))

                InitializeComponents()
            End Set
        End Property

        Public Property ActiveViewIndex As Integer
            Get
                Return CourseDetailControlViews.ActiveViewIndex
            End Get
            Set(value As Integer)
                CourseDetailControlViews.ActiveViewIndex = value
            End Set
        End Property

        Private Property Presenter As CourseDetailControlPresenter
        Private Property CssClass As String
        Private Property IsReadOnly As Boolean

#End Region

#Region "ICourseDetailControlView Members"

        Public Property Code As String Implements ICourseDetailControlView.Code
            Get
                Return CodeTextBox.Text
            End Get
            Set(value As String)
                CodeTextBox.Text = value
            End Set
        End Property

        Public ReadOnly Property CourseId As Integer Implements ICourseDetailControlView.CourseId
            Get
                Dim id As Integer = 0
                Dim cid As String = HttpContext.Current.Request.QueryString("cid")
                If Not String.IsNullOrEmpty(cid) AndAlso Integer.TryParse(cid, id) Then
                    If SISSATPageBase.User.Employee.EmployeeType.EmployeeTypeId < 3 Then
                        ControlMode = PageModes.EditMode
                        ActiveViewIndex = 1
                    Else
                        ControlMode = PageModes.ViewMode
                    End If
                ElseIf String.Compare(cid, "new", True) = 0 Then
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                End If

                Return id
            End Get
        End Property

        Public ReadOnly Property CurrentUserId As Integer Implements ICourseDetailControlView.CurrentUserId
            Get
                Return SISSATPageBase.User.Employee.EmployeeId
            End Get
        End Property

        Public Property Description As String Implements ICourseDetailControlView.Description
            Get
                Return DescriptionTextBox.Text
            End Get
            Set(value As String)
                DescriptionTextBox.Text = value
            End Set
        End Property

        Public Property Name As String Implements ICourseDetailControlView.Name
            Get
                Return NameTextBox.Text
            End Get
            Set(value As String)
                NameTextBox.Text = value
            End Set
        End Property

        Public Event OnClear() Implements ICourseDetailControlView.OnClear
        Public Event OnInitialization() Implements ICourseDetailControlView.OnInitialization
        Public Event OnSubmit() Implements ICourseDetailControlView.OnSubmit

#End Region

#Region "Methods"

        Private Sub InitializeComponents()
            ActiveViewIndex = Convert.ToInt32(IIf(IsReadOnly, 0, 1))
            NameTextBox.ReadOnly = IsReadOnly
            NameTextBox.CssClass = CssClass
            DescriptionTextBox.ReadOnly = IsReadOnly
            CodeTextBox.ReadOnly = IsReadOnly
            CodeTextBox.CssClass = CssClass
        End Sub

#End Region

#Region "Events"

        Private Sub PageInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            Presenter = New CourseDetailControlPresenter(Me)
            RaiseEvent OnInitialization()
        End Sub

        Private Sub ClearButtonClick(sender As Object, e As EventArgs) Handles ClearButton.Click
            RaiseEvent OnClear()
            Me.ErrorMessage = "Reset of data is successful."
        End Sub

        Private Sub SubmitButtonClick(sender As Object, e As EventArgs) Handles SubmitButton.Click
            RaiseEvent OnSubmit()
            Me.ErrorMessage = "Save is successful."
        End Sub

#End Region

    End Class
End Namespace
